CERT_HOME=/home/lorry/cn_shell/cfssl
cd $CERT_HOME

echo " ***************************************"
echo " ******* Build CA json config *********"
echo " ***************************************"
cat << EOF > ca-csr.json
{
    "CN": "My own CA",
    "key": {
        "algo": "rsa",
        "size": 2048
    },
    "names": [
        {
            "C": "US",
            "L": "CA",
            "O": "My Company Name",
            "ST": "San Francisco",
            "OU": "Org Unit 1",
            "OU": "Org Unit 2"
        }
    ]
}
EOF

cat << EOF > ca-config.json
{
    "signing": {
        "default": {
            "expiry": "43800h"
        },
        "profiles": {
            "server": {
                "expiry": "43800h",
                "usages": [
                    "signing",
                    "key encipherment",
                    "server auth",
                    "client auth"
                ]
            },
            "client": {
                "expiry": "43800h",
                "usages": [
                    "signing",
                    "key encipherment",
                    "client auth"
                ]
            },
            "peer": {
                "expiry": "43800h",
                "usages": [
                    "signing",
                    "key encipherment",
                    "server auth",
                    "client auth"
                ]
            }
        }
    }
}

EOF

echo " ***********************************"
echo " ******* Generated CA Cert *********"
echo " ***********************************"

cfssl gencert -initca ca-csr.json | cfssljson -bare ca -

echo " ***********************************"
echo " ***** Generated Server Cert *******"
echo " ***********************************"

echo '{"CN":"cn1","hosts":["192.168.1.165","127.0.0.1","cn1"],"key":{"algo":"rsa","size":2048}}' | \
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=server -hostname="192.168.1.165,127.0.0.1,cn1" - | \
cfssljson -bare server

echo " ***********************************"
echo " ***** Generated Member Cert *******"
echo " ***********************************"

echo '{"CN":"member1","hosts":["192.168.1.165","127.0.0.1","cn1"],"key":{"algo":"rsa","size":2048}}' | \
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=peer -hostname="192.168.1.165,127.0.0.1,cn1,member1" - | \
cfssljson -bare member1

echo " ***********************************"
echo " ***** Generated Client Cert *******"
echo " ***********************************"

echo '{"CN":"client","hosts":["192.168.1.165","127.0.0.1","cn1"],"key":{"algo":"rsa","size":2048}}' | \
cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=client - | \
cfssljson -bare client